//
//  CZNetWorkTools.swift
//  weiBo
//
//  Created by apple on 16/5/12.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import AFNetworking

enum RequestType:Int{
    case GET = 0
    case POST = 1

}

class CZNetWorkTools: AFHTTPSessionManager {

    static let shareTools : CZNetWorkTools = {
        let tools = CZNetWorkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tools
    }()
    
    //封装上传的请求


    func requestUpload(url: String, parmas: AnyObject,data: NSData,name: String,callback:(response : AnyObject?,error : NSError?)->()) {
    
        POST(url, parameters: parmas, constructingBodyWithBlock: { (formData) -> Void in
        formData.appendPartWithFileData(data, name: name, fileName: "test", mimeType: "application/octet-stream")
        },progress: nil, success: { (_, response) -> Void in
            callback(response: response, error: nil)
        }) { (_, error) -> Void in
            callback(response: nil, error: error)
                
        }
            
    }
    
    




    func request(requestType : RequestType, url:String, params:AnyObject?, callback:(response : AnyObject?,error : NSError?)->()){
        
        let successCloseSure = {(dataTask : NSURLSessionDataTask , response : AnyObject?) -> Void in
            callback(response: response, error: nil)
        }
        
        let failureCloseSure = {(dataTask : NSURLSessionDataTask? , error : NSError) -> Void in
            callback(response: nil, error: error)
        }
        
        if requestType == .GET{
            GET(url, parameters: params, progress: nil, success: successCloseSure, failure: failureCloseSure)
        }else{
            POST(url, parameters: params, progress: nil, success: successCloseSure, failure: failureCloseSure)
        }
    }


}
//首页相关接口
extension CZNetWorkTools{
    func requestStatus(accessToken:String, maxId: Int64, sinceId:Int64, callBack:(response : AnyObject?,error :NSError?) -> ()){
        //创建地址
        let url = "https://api.weibo.com/2/statuses/friends_timeline.json"
        //设置参数
        let params = [
            "access_token": accessToken,
            "max_id": "\(maxId)",
            "since_id": "\(sinceId)"
        ]
        request(.GET, url: url, params: params, callback: callBack)
    
    
    }


}

extension CZNetWorkTools
{
    //  获取accesstoken的接口
    func requestAccessToken(code : String,callback:(response : AnyObject?,error : NSError?)->())
    {
        //获取网址
        let url = "https://api.weibo.com/oauth2/access_token"
        //获取参数
        let params = [
            "client_id": weiboAppKet,
            "client_secret": WeiboAppSecret,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": WeiboRedirect_Uri
        ]
        
        request(.POST, url: url, params: params, callback: callback)
    }
    
    //获取用户信息
    func requestUserInfo(userAccount : CZUserAcount,callback:(response : AnyObject?,error : NSError?)->()){
        let url = "https://api.weibo.com/2/users/show.json"
        let params = [
            "access_token": userAccount.access_token!,
            "uid": "\(userAccount.uid)"
        ]
//        print(userAccount.access_token!)
        
        request(.GET, url: url, params: params, callback: callback)        
    }

    

}

//发送图片接口
extension CZNetWorkTools {
        func upload(access_token: String, status: String,image: UIImage, callBack: (response: AnyObject?, error: NSError?)->()){
        
            let url = "https://upload.api.weibo.com/2/statuses/upload.json"
            let params = [
                "access_token": access_token,
                "status": status
            ]
            
            //将图片转换成二进制
            let data = UIImageJPEGRepresentation(image, 0.6)!
            
            requestUpload(url, parmas: params, data: data, name: "pic", callback: callBack)
    
    }

}

//发微博接口
extension CZNetWorkTools{
    func update(access_token: String, status:String, callBack:(response: AnyObject?, error: NSError?) -> ()) {
        
        let url = "https://api.weibo.com/2/statuses/update.json"
        let params = [
            "access_token": access_token,
            "status": status
        ]
        
        request(.POST, url: url, params: params, callback: callBack)
    }


}


